import React from "react";
// comp
import { Routes, Route, Navigate, useNavigate } from "react-router-dom";
import { getToken } from "./../utils/index";

const AuthorityRoute = ({ props }) => {
  const routes = [...props];
  // 判断是否有token
  // const navigate = useNavigate();
  // if (!getToken()) {
  //   const noAuthRoutes = routes.filter((i) => i.noAuth);
  //   // 渲染页面
  //   return <Routes>{mapRoute(noAuthRoutes)}</Routes>;
  // }
  // 渲染页面
  return (
    <Routes>
      <Route index exact element={<Navigate to="/home" />}></Route>
      {mapRoute(routes)}
    </Routes>
  );
};

const mapRoute = (routes) => {
  return routes.map((m) => {
    if (m.component) {
      return <Route exact key={m.path} path={m.path} element={m.component} />;
    }
    if (m.children) {
      return mapRoute(m.children);
    }
  });
};

export default AuthorityRoute;
